package com.xsq.wifi.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.xsq.wifi.R;
import com.xsq.wifi.model.DeviceModel;
import com.xsq.wifi.utils.ToastUtil;

import java.util.List;

/**
 * 编写人：李振德
 * 创建时间：2025/5/13
 */
public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder>{

    public List<DeviceModel> deviceModelList;
    private Activity mactivity;
    private ViewHolder viewHolder;

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView status_img;
        View isline;
        TextView isline_text,mac_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            status_img = (ImageView) itemView.findViewById(R.id.status_img);
            isline = (View) itemView.findViewById(R.id.isline);
            isline_text = (TextView) itemView.findViewById(R.id.isline_text);
            mac_text = (TextView) itemView.findViewById(R.id.mac_text);
        }
    }
    public DeviceAdapter(List<DeviceModel> lostList, Activity activity){
        deviceModelList = lostList;
        mactivity = activity;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        DeviceModel deviceModel = deviceModelList.get(position);
        if(!deviceModel.isIsonline()){
            Glide.with(mactivity)
                    .load(R.mipmap.device_unline_img)
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(holder.status_img);
            holder.isline.setBackgroundResource(R.drawable.circle_dot_hui);
            holder.isline_text.setText("离线");
        }else{
            Glide.with(mactivity)
                    .load(R.mipmap.device_online_img)
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(holder.status_img);
            holder.isline.setBackgroundResource(R.drawable.circle_dot_green);
            holder.isline_text.setText("在线");
        }
        holder.mac_text.setText(deviceModel.getDevicemac());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.isline_text.getText().toString().equals("离线")){
                    ToastUtil.show(mactivity,"离线不可操作");
                }else{
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(deviceModel.getDeviceip(),deviceModel.getDeviceuuid());
                    }
                }

            }
        });
    }

    /**
     * 添加或更新设备（根据UUID判断是否存在）
     * @param newDevice 新设备数据
     */
    public void addOrUpdateDevice(DeviceModel newDevice) {
        // 查找是否已存在相同UUID的设备
        int existingIndex = -1;
        for (int i = 0; i < deviceModelList.size(); i++) {
            if (deviceModelList.get(i).getDeviceuuid().equals(newDevice.getDeviceuuid())) {
                existingIndex = i;
                break;
            }
        }

        if (existingIndex >= 0) {
            // 更新已有设备
            deviceModelList.set(existingIndex, newDevice);
            notifyItemChanged(existingIndex);
        } else {
            // 添加新设备
            deviceModelList.add(newDevice);
            notifyItemInserted(deviceModelList.size() - 1);
        }
    }

    @Override
    public int getItemCount() {
        return deviceModelList.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(String ip,String uuid);
    }

}
